//                        date: [[20120731120157]]
//                        Author: Ahmed Fathi Hamouda Abd El Hafez
using System;
using System.ComponentModel;
using System.Reflection;
using System.Data;
using System.Diagnostics;
using CustomersSuppliersDataLayer;
using System.Collections.Generic;


namespace CustomersSuppliersBusinessLayer
{

    /// <summary>
    ///  Business Object OF Table DealersClassifications
    /// </summary>

    public partial class BLDealersClassification : INotifyPropertyChanged
    {
        #region Fields
        private BLDealersClassification.BusinessRelatedCollections f_RelatedCollections = null;
        #endregion

        #region properties
        /// <summary>
        /// RelatedCollections of BLDealersClassification
        /// </summary>
        [Browsable(false)]
        public BLDealersClassification.BusinessRelatedCollections RelatedCollections
        {
            get
            {
                if (this.f_RelatedCollections == null)
                {
                    this.f_RelatedCollections = new BusinessRelatedCollections(this);
                }
                return f_RelatedCollections;
            }
            set
            {
                f_RelatedCollections = value;
            }
        }
        #endregion

        #region methods
        /// <summary>
        /// test that object dosn't have any relations and can be deleted
        /// </summary>
        public bool IsDeleteReady()
        {
            bool hasRelations = false;
            hasRelations = hasRelations || (this.RelatedCollections.CollectionDealerByDealerClassification_ID.Count > 0);
            return !hasRelations;
        }
        #endregion

        #region RelatedCollections
        /// <summary>
        /// contains properties of the Related Collections 
        /// </summary>
        [Serializable]
        public class BusinessRelatedCollections
        {
            private BLDealersClassification containerObject = null;
            private Dictionary<string, List<string>> RelatedCollectionsMappings = new Dictionary<string, List<string>>();

            private BLDealerCollection f_CollectionDealerByDealerClassification_ID = null;

            /// <summary>
            /// returns  BLDealerCollections that depend on this DealerClassification_ID property
            /// </summary>
            public BLDealerCollection CollectionDealerByDealerClassification_ID
            {
                get
                {
                    if (this.f_CollectionDealerByDealerClassification_ID == null)
                    {
                        this.f_CollectionDealerByDealerClassification_ID = new BLDealerCollection();
                        if (containerObject.DealerClassification_ID != null)
                        {
                            this.f_CollectionDealerByDealerClassification_ID.Template.DealerClassification_ID = containerObject.DealerClassification_ID;
                            this.f_CollectionDealerByDealerClassification_ID.LoadByProperties(new string[] { BLDealersClassification.PropertiesNames.DealerClassification_ID });
                        }
                    }
                    return this.f_CollectionDealerByDealerClassification_ID;
                }
            }


            public BusinessRelatedCollections(BLDealersClassification item)
            {
                this.containerObject = item;
                if (this.containerObject != null)
                {
                    this.containerObject.PropertyChanged += new PropertyChangedEventHandler(this.containerObject_PropertyChanged);
                    this.containerObject.AfterLoad += new EventHandler(this.containerObject_AfterLoad);
                }
                if (!RelatedCollectionsMappings.ContainsKey(BLDealersClassification.PropertiesNames.DealerClassification_ID))
                {
                    this.RelatedCollectionsMappings.Add(BLDealersClassification.PropertiesNames.DealerClassification_ID, new List<string>());
                }
                this.RelatedCollectionsMappings[BLDealersClassification.PropertiesNames.DealerClassification_ID].Add("f_CollectionDealerByDealerClassification_ID");
            }

            public void ResetProperties(string[] propertiesNames)
            {
                for (int i = 0; i < propertiesNames.Length; i++)
                {
                    if (this.RelatedCollectionsMappings.ContainsKey(propertiesNames[i]))
                    {
                        for (int j = 0; j < this.RelatedCollectionsMappings[propertiesNames[i]].Count; j++)
                        {
                            string FieldName = this.RelatedCollectionsMappings[propertiesNames[i]][j];
                            this.GetType().GetField(FieldName, BindingFlags.NonPublic).SetValue(this, null);
                        }
                    }
                }
            }


            private void containerObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                this.ResetProperties(new string[] { e.PropertyName });
            }
            private void containerObject_AfterLoad(object sender, EventArgs e)
            {
                string[] propertiesNames = new string[this.RelatedCollectionsMappings.Keys.Count];
                this.RelatedCollectionsMappings.Keys.CopyTo(propertiesNames, 0);

                this.ResetProperties(propertiesNames);
            }
        }
        #endregion

    }
}
